package com.zhongyouyun.lbyc.service.imp;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.zhongyouyun.admin.model.UserModel;
import com.zhongyouyun.lbyc.excel.CustomReputationLevelExcel;
import com.zhongyouyun.lbyc.excel.CustomTrackingNumberExcel;
import com.zhongyouyun.lbyc.model.CustomReputationLevelModel;
import com.zhongyouyun.lbyc.model.CustomTrackingNumberModel;
import com.zhongyouyun.lbyc.mapper.CustomTrackingNumberMapper;
import com.zhongyouyun.lbyc.pojo.CustomModernBrandWidthPojo;
import com.zhongyouyun.lbyc.pojo.CustomTrackingNumberPojo;
import com.zhongyouyun.lbyc.service.CustomTrackingNumberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongyouyun.other.exception.BizException;
import com.zhongyouyun.utils.ExcelUtils;
import com.zhongyouyun.utils.ModelUtils;
import com.zhongyouyun.utils.QueryUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author: shenyi
 * @Date: 2023-12-01
 * @Description: 持续跟踪消费者会员数量服务实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CustomTrackingNumberServiceImpl extends ServiceImpl<CustomTrackingNumberMapper, CustomTrackingNumberModel> implements CustomTrackingNumberService {
    /*
     * @Author: shenyi
     * @Date: 2021-09-08
     * @Description: 分页查询
     * @param page 分页数据  lambdaQueryWrapper where 条件 和排序
     */
    @Override
    public IPage<CustomTrackingNumberModel> selectPage(Page<CustomTrackingNumberModel> page, QueryWrapper<CustomTrackingNumberModel> queryWrapper) {
        return this.baseMapper.selectPage(page, queryWrapper);
    }


    /*
     * @Author: shenyi
     * @Date: 2021-09-08
     * @Description: 查询全部
     * @param   lambdaQueryWrapper where 条件 和排序
     */
    @Override
    public List<CustomTrackingNumberModel> selectList(QueryWrapper<CustomTrackingNumberModel> queryWrapper) {
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<CustomTrackingNumberPojo> selectPojoPage(Page page, QueryWrapper<CustomTrackingNumberPojo> queryWrapper) {
        return this.baseMapper.selectPojoPage(page, queryWrapper);

    }
    @Override
    @Transactional
    public Boolean export(MultipartFile file, String quarter) {
        Boolean flag = false;
        UserModel model = (UserModel) SecurityUtils.getSubject().getPrincipal(); // 获取当前登录用户
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.in("dept_id", QueryUtils.findChaDeptIds(model));
            queryWrapper.eq("quarter", quarter);
            remove(queryWrapper);
            List<CustomTrackingNumberExcel> customTrackingNumberExcelList = (List<CustomTrackingNumberExcel>) ExcelUtils.ReadExcelByAll(file, 0, -1, 1, CustomTrackingNumberExcel.class);
            customTrackingNumberExcelList.removeIf(customTrackingNumberExcel -> ObjectUtil.isEmpty(customTrackingNumberExcel.getCustomCode()));
            if (customTrackingNumberExcelList.size() > 0) {
                List<CustomTrackingNumberModel> customTrackingNumberModelList = BeanUtil.copyToList(customTrackingNumberExcelList, CustomTrackingNumberModel.class);
                ModelUtils modelutils = new ModelUtils();
                customTrackingNumberModelList.forEach(customTrackingNumberModel -> {
                    customTrackingNumberModel.setQuarter(quarter);
                    customTrackingNumberModel.setDeptId(model.getDeptId());
                    modelutils.initModel(customTrackingNumberModel, model.getId().toString());

                });
                saveBatch(customTrackingNumberModelList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException(1, "导入错误", false);
        }
        return flag;
    }
}
